package com.oa.modules.approvalsec1.controller;

import com.oa.common.annotation.OperationLog;
import com.oa.common.utils.*;
import com.oa.modules.approvalsec1.entity.CompletAccept;
import com.oa.modules.approvalsec1.service.CompletAcceptService;
import com.oa.modules.approvalsec1.service.OperationlogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * (CompletAccept)表控制层
 *
 * @author makejava
 * @since 2020-11-18 10:49:10
 */
@RestController
@RequestMapping("completAccept")
public class CompletAcceptController {
    /**
     * 服务对象
     */
    @Resource
    private CompletAcceptService completAcceptService;
    @Autowired
    private OperationlogService operationlogService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public CompletAccept selectOne(Long id) {
        return this.completAcceptService.queryById(id);
    }

    /**
     * 分页查询
     * @param params
     * @return
     */
    @PostMapping(value = "/queryPage")
    @RequiresPermissions("approvalSec1:completaccept:list")
    @OperationLog("竣工验收")
    public R queryPage(@RequestParam Map<String, Object> params){
        PageUtils page = completAcceptService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 保存日志
     * @param completAccept
     * @return
     */
    @PostMapping(value = "/saveOperationLog")
    public R saveOperationLog(@RequestBody CompletAccept completAccept){
        String operation = "竣工验收：查看工程名称为："+completAccept.getEngineerName()+"，业务名称为："+completAccept.getBusinessName()+"的详情";
        operationlogService.packOperation(operation);
        return R.ok();
    }

    @PostMapping(value = "/export")
    public void export(@RequestBody CompletAccept completAccept, HttpServletResponse response){
        List<CompletAccept> records = completAcceptService.queryAll(completAccept);
        LinkedHashMap<String, String> titleMap = getTitleMap();
        try {
            ExcelUtils.listToExcel(records, titleMap, "竣工验收", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * d导出标题栏
     *
     * @return
     */
    private static LinkedHashMap<String, String> getTitleMap() {
        LinkedHashMap<String, String> names = new LinkedHashMap<String, String>();
        //name和operator是ExcelDTO实体类的属性名，姓名表示name列表头显示的名称
        names.put("businessName", "业务名称");
        names.put("engineerName", "工程名称");
        names.put("projectCode", "项目代码");
        names.put("socialCode", "统一社会代码");
        names.put("projectLeader", "项目负责人");
        names.put("leaderIdcard", "身份证号");
        names.put("leaderPhone", "移动电话");
        return names;
    }

    @PostMapping(value = "/getDoc")
    public void getDoc(@RequestBody CompletAccept completAccept,HttpServletResponse response) {
        List<CompletAccept> records = completAcceptService.queryAll(completAccept);
        if(records!=null && records.size()>0){
            Map<String,Object> dataMap=new HashMap<String,Object>();
            dataMap.put("title", "竣工验收");
            List<Map<String,Object>> list = new ArrayList<>();
            if(records!=null && records.size()>0){
                for(int i=0;i<records.size();i++){
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("businessName",records.get(i).getBusinessName());
                    map.put("engineerName",records.get(i).getEngineerName());
                    map.put("projectCode",records.get(i).getProjectCode());
                    map.put("socialCode",records.get(i).getSocialCode());
                    map.put("projectLeader",records.get(i).getProjectLeader());
                    map.put("leaderIdcard",records.get(i).getLeaderIdcard());
                    map.put("leaderPhone",records.get(i).getLeaderPhone());
                    list.add(map);
                }
            }
            dataMap.put("list", list);
            String templateName="completaccept.ftl";
            String filePath="D:\\freemarker";
            String fileName="竣工验收"+System.currentTimeMillis()+".doc";
            try {
                FreemarkerUtil.createWord(dataMap,templateName,filePath,fileName,response);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("文件下载失败");
            }
        }
    }
}
